import { Inject, Injectable } from '@nestjs/common';
import { EmailParams } from 'src/types';
import * as nodemailer from 'nodemailer';
import { ConfigService } from '@nestjs/config';

@Injectable()
export class EmailService {
  @Inject()
  private readonly configService: ConfigService;

  /**
   * 发送邮件
   * @param to 收件人
   * @param title 标题
   * @param content 内容
   */
  async sendEmail({
    to,
    title = '系统消息',
    content,
  }: EmailParams): Promise<void> {
    const transport = nodemailer.createTransport({
      host: 'smtp.qq.com',
      port: 465,
      secure: true,
      auth: {
        user: this.configService.get('EMAIL_USER'),
        pass: this.configService.get('EMAIL_PASS'),
      },
    });
    await transport.sendMail({
      from: this.configService.get('EMAIL_USER'),
      to,
      subject: title,
      text: content,
    });
  }
}
